package com.atguigu.tingshu.order.service.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.context.OrderHandlerInterceptorContext;
import com.atguigu.tingshu.order.context.OrderTradeStrategyContext;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;


    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {
        // 根据付款项目获取对应的策略对象
        OrderTradeStrategy orderTradeStrategy = OrderTradeStrategyContext.getOrderTradeStrategy(tradeVo.getItemType());

        // 调用策略对象的方法获取对应的响应结果数据
        return orderTradeStrategy.trade(tradeVo);
    }

    @Override
    public String submitOrder(OrderInfoVo orderInfoVo) {

        OrderInfoVo orderInfoVo1 = OrderHandlerInterceptorContext.entry(orderInfoVo);
        return orderInfoVo1.getTradeNo();
    }

    @Override
    public void cancelOrder(String orderNo) {
        LambdaQueryWrapper<OrderInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderInfo::getOrderNo,orderNo);
        OrderInfo orderInfo = orderInfoMapper.selectOne(queryWrapper);
        orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_CANCEL);
        queryWrapper.eq(OrderInfo::getOrderStatus, SystemConstant.ORDER_STATUS_UNPAID);
        orderInfoMapper.update(orderInfo, queryWrapper);
        log.info("订单{}取消的延迟任务执行了。。。",orderNo);
    }
}
